﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PulseLibrary;
using Microsoft.Xna.Framework.Input;
using PulsePrototype.Messages.PhysicsMessages;
using FarseerPhysics;

namespace PulsePrototype.Components
{
    public enum Movement
    {
        Left,
        Right,
        Up,
        Down,
        Stop
    }

    public class ControlComponent : BaseComponent
    {
        Movement currentMoveX = Movement.Stop;
        Movement currentMoveY = Movement.Stop;

        protected override String GetName()
        {
            return "Control";
        }

        public ControlComponent(BaseEntity ParentEntity) :
            base(ParentEntity)
        {
            Initialize();
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        public void Move()
        {
            switch(currentMoveX)
            {
                case Movement.Left:
                    //MsgSetWheelSpeed msg1 = new MsgSetWheelSpeed(Parent.UniqueID, -1.0f);
                    MsgSetVelocity msg1 = new MsgSetVelocity(Parent.UniqueID, ConvertUnits.ToSimUnits(-1000.0f), 0, 0);
                    Parent.Manager.SendMessage(msg1);
                    break;
                case Movement.Right:
                    //MsgSetWheelSpeed msg2 = new MsgSetWheelSpeed(Parent.UniqueID, 1.0f);
                    MsgSetVelocity msg2 = new MsgSetVelocity(Parent.UniqueID, ConvertUnits.ToSimUnits(1000.0f), 0, 0);
                    Parent.Manager.SendMessage(msg2);
                    break;
                case Movement.Stop:
                //MsgSetWheelSpeed msg3 = new MsgSetWheelSpeed(Parent.UniqueID, 0.0f);
                //Parent.Manager.SendMessage(msg3);
                break;
            }

            switch(currentMoveY)
            {
                case Movement.Up:
                    //MsgSetWheelSpeed msg2 = new MsgSetWheelSpeed(Parent.UniqueID, 1.0f);
                    MsgSetVelocity msg3 = new MsgSetVelocity(Parent.UniqueID, 0, ConvertUnits.ToSimUnits(-1000.0f), 0);
                    Parent.Manager.SendMessage(msg3);
                    break;
                case Movement.Down:
                    //MsgSetWheelSpeed msg2 = new MsgSetWheelSpeed(Parent.UniqueID, 1.0f);
                    MsgSetVelocity msg4 = new MsgSetVelocity(Parent.UniqueID, 0, ConvertUnits.ToSimUnits(1000.0f), 0);
                    Parent.Manager.SendMessage(msg4);
                    break;
                case Movement.Stop:
                    //MsgSetWheelSpeed msg3 = new MsgSetWheelSpeed(Parent.UniqueID, 0.0f);
                    //Parent.Manager.SendMessage(msg3);
                    break;
            }
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (InputManager.KeyDown(Keys.Left)) {
                currentMoveX = Movement.Left;
            }
            else if (InputManager.KeyDown(Keys.Right)) {
                currentMoveX = Movement.Right;
            }
            else {
                currentMoveX = Movement.Stop;
            }

            if (InputManager.KeyDown(Keys.Up)) {
                currentMoveY = Movement.Up;
            }
            else if (InputManager.KeyDown(Keys.Down)) {
                currentMoveY = Movement.Down;
            }
            else {
                currentMoveY = Movement.Stop;
            }

            Move();
            base.Update(gameTime);
        }
    }
}
